package cn.ai.shoes.client.job;

import cn.ai.shoes.client.entity.CurrentServerEntity;
import cn.ai.shoes.client.utils.LoadBalanceStrategy;
import cn.ai.shoes.common.exception.ServiceException;
import cn.ai.shoes.common.exception.constant.GlobalErrorCodeConstants;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ComfyuiTask {
    @Resource
    private CurrentServerEntity currentServerEntity;
    @Resource
    private LoadBalanceStrategy loadBalanceStrategy;
    /**
     * 定时任务 切换服务器
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void switchComfyuiServer() {
        log.info("定时任务----切换服务器");
        String newServer = loadBalanceStrategy.switchServer();
        if (newServer == null) {
            throw new ServiceException(GlobalErrorCodeConstants.SERVER_NOT_AVAILABLE);
        }
        currentServerEntity.setUrl(newServer);
        log.info("服务器已切换为: {}", newServer);
    }
}
